﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.IBLL;
using KPIS.GERP.GIMS.MODEL;

namespace KPIS.GERP.GIMS.APP.APP_UI.Form
{
    public partial class ICM_Master_Purchase_Method : System.Web.UI.Page
    {
        Common.Main cn = new Common.Main();
        CommandControl cmdFac = new CommandControl();
        IBLLMasterPurchaseType bllType = new MasterPurchaseTypeBLL();
        IBLLMasterPurchaseMethod bllMethod = new MasterPurchaseMethodBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDataGridPurchaseType();
                panelCommand.Visible = false;
                panelData.Visible = false;
            }
        }

        public void LoadDataGridPurchaseType()
        {
            IList<MasterPurchaseTypeInfo> info = bllType.GetMasterPurchaseType();
            GridPurchaseType.DataSource = info;
            GridPurchaseType.DataBind();
        }

        public void LoadDataGrid(int type_seq)
        {
            IList<MasterPurchaseMethodInfo> info = bllMethod.GetMasterPurchaseMethodWithType(type_seq);
            Grid.DataSource = info;
            Grid.DataBind();

            cmdFac.GridCommand(panelList, panelGrid, panelCommand);
        }

        public void GetKeyValue(string seq)
        {
            if (seq.Length > 0)
            {
                MasterPurchaseMethodInfo info = bllMethod.GetMasterPurchaseMethod(Convert.ToInt32(seq));

                txtName.Text = info.Name;
                txtDescr.Text = info.Descr;
                rdbBidding.SelectedValue = info.Bidding_status;

                cmdFac.ViewCommand(panelList, panelGrid, panelCommand);
            }
        }

        protected void AddNew_Click(object sender, EventArgs e)
        {
            cmdFac.AddNewCommand(panelList, panelGrid, panelCommand);
        }

        protected void Edit_Click(object sender, EventArgs e)
        {
            cmdFac.EditCommand(panelList, panelGrid, panelCommand);
        }

        protected void Delete_Click(object sender, EventArgs e)
        {
            IList<MasterPurchaseMethodInfo> infoList = new List<MasterPurchaseMethodInfo>();
            if (panelGrid.Visible)
            {
                foreach (GridDataItem data in Grid.MasterTableView.GetSelectedItems())
                {
                    MasterPurchaseMethodInfo info = new MasterPurchaseMethodInfo();
                    RecordInfo infoRecord = new RecordInfo();
                    info.Seq = Convert.ToInt32(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Seq"]);
                    infoRecord.Updated_by = Int32.Parse(Session["EmpSeq"].ToString());
                    infoRecord.Updated_when = DateTime.Now;
                    info.InfoRecord = infoRecord;
                    infoList.Add(info);
                }
            }
            else
            {
                MasterPurchaseMethodInfo info = new MasterPurchaseMethodInfo();
                RecordInfo infoRecord = new RecordInfo();
                info.Seq = Convert.ToInt32(hdSeq.Value);
                infoRecord.Updated_by = Int32.Parse(Session["EmpSeq"].ToString());
                infoRecord.Updated_when = DateTime.Now;
                info.InfoRecord = infoRecord;
                infoList.Add(info);
            }

            if (infoList.Count == 0)
            {
                RadManager1.Alert(SystemInfo._DeleteNull);
                return;
            }

            try
            {
                bllMethod.DeleteAll(infoList);
            }
            catch
            {
                RadManager1.Alert(SystemInfo._DeleteError);
                return;
            }

            LoadDataGrid(Convert.ToInt32(hdType.Value));
        }

        protected void AddNewSave_Click(object sender, EventArgs e)
        {
            MasterPurchaseMethodInfo info = new MasterPurchaseMethodInfo();
            MasterPurchaseTypeInfo infoType = new MasterPurchaseTypeInfo();
            RecordInfo infoRecord = new RecordInfo();
            info.Name = txtName.Text;
            info.Descr = txtDescr.Text;
            info.Reason = txtReason.Text;
            infoType.Seq = Convert.ToInt32(hdType.Value);
            info.Bidding_status = rdbBidding.SelectedValue;
            infoRecord.Created_by = cn.UserID;
            infoRecord.Created_when = DateTime.Now;
            info.InfoType = infoType;
            info.InfoRecord = infoRecord;

            if (ValtxtName.IsValid == false)
            {
                return;
            }

            try
            {
                bllMethod.Insert(info);
            }
            catch
            {
                RadManager1.Alert(SystemInfo._InsertError);
                return;
            }
            LoadDataGrid(Convert.ToInt32(hdType.Value));
        }

        protected void EditSave_Click(object sender, EventArgs e)
        {
            MasterPurchaseMethodInfo info = new MasterPurchaseMethodInfo();
            MasterPurchaseTypeInfo infoType = new MasterPurchaseTypeInfo();
            RecordInfo infoRecord = new RecordInfo();
            info.Seq = Convert.ToInt32(hdSeq.Value);
            info.Name = txtName.Text;
            info.Descr = txtDescr.Text;
            info.Reason = txtReason.Text;
            infoType.Seq = Convert.ToInt32(hdType.Value);
            info.Bidding_status = rdbBidding.SelectedValue;
            infoRecord.Updated_by = Int32.Parse(Session["EmpSeq"].ToString());
            infoRecord.Updated_when = DateTime.Now;
            info.InfoType = infoType;
            info.InfoRecord = infoRecord;

            if (ValtxtName.IsValid == false)
            {
                return;
            }

            try
            {
                bllMethod.Update(info);
            }
            catch
            {
                RadManager1.Alert(SystemInfo._InsertError);
                return;
            }
            GetKeyValue(hdSeq.Value);
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            cmdFac.CancelCommand(panelList, panelGrid, panelCommand);
        }

        protected void ShowGrid_Click(object sender, EventArgs e)
        {
            LoadDataGrid(Convert.ToInt32(hdType.Value));
        }

        protected void Grid_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadDataGrid(Convert.ToInt32(hdType.Value));
        }

        protected void RadManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            if (e.Argument == "Type")
            {
                LoadDataGrid(Convert.ToInt32(hdType.Value));
                MasterPurchaseTypeInfo info = bllType.GetMasterPurchaseType(Convert.ToInt32(hdType.Value));
                SetHeader(info.Name);
                panelBlank.Visible = false;
                panelCommand.Visible = true;
                panelData.Visible = true;
            }
            else
            {
                GetKeyValue(e.Argument);
            }
        }

        private void SetHeader(string header)
        {
            lblHeaderList.Text = "วิธีการจัดหาจากประเภท [" + header + "]";
        }

        protected void OnAjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            RadAjaxPanel1.Controls.Add(Page.LoadControl("../UserControl/Personal_Data.ascx"));
        }

        protected void clearFilter_Click(object sender, EventArgs e)
        {
            foreach (GridColumn column in Grid.MasterTableView.Columns)
            {
                column.CurrentFilterFunction = GridKnownFunction.NoFilter;
                column.CurrentFilterValue = string.Empty;
            }
            Grid.MasterTableView.FilterExpression = string.Empty;
            LoadDataGrid(Convert.ToInt32(hdType.Value));
        }
    }
}
